//
// Created by rebeater on 3/23/21.
//

#include "Sort.h"

void printDouble(double d, uint8_t n) {
  if (d < 0) {
	printf("-");
	printDouble(-d, n);
	return;
  }
  int di = (int)d;
  double dd = d - di;
  uint64_t ddi = (uint64_t)(pow(10, n) * dd);
  char buff[10];
  sprintf(buff, "%%d.%%0%dlu", n);
  printf(buff, di, ddi);
}

void sprintDouble(char *buffer, double d, uint8_t n) {
  if (d < 0) {
	sprintf(buffer, "-");
	sprintDouble(buffer + 1, -d, n);
	return;
  }
  int di = (int)d;
  double dd = d - di;
  uint64_t ddi = (uint64_t)(pow(10, n) * dd);
  char buff[12];
  sprintf(buff, "%%d.%%0%dllu ", n);
  sprintf(buffer, buff, di, ddi);
}


#include "Timer.h"

namespace rebeater {
void testSort() {
  const int len = 1e5;
  std::vector<int> v1, v2;

  srand(time(NULL));
  for (int i = 0; i < len; i++) {
	v1.push_back(rand());
  }
  Timer timer;
  timer.reset();
  Sort::MergeSort(v1);
  cout << "merge:" << timer.elapsed_micro()<<"\t";
  cout << (Sort::check(v1)?"OK":"Failed" )<<endl;

  srand(time(NULL));
  for (int i = 0; i < len; i++) {
	v2.push_back(rand());
  }
  timer.reset();
  Sort::InsertSort(v2);
  cout << "insert:" << timer.elapsed_micro() << "\t";
  cout << (Sort::check(v2)?"OK":"Failed" )<< endl;

  v2.clear();
  srand(time(NULL));
  for (int i = 0; i < len; i++) {
	v2.push_back(rand());
  }
  timer.reset();
  Sort::QuickSort(v2);
  cout << "quick:" << timer.elapsed_micro() <<"\t";
  cout << (Sort::check(v2)?"OK":"Failed" )<< endl;

}
}
